import { webMercatorProject, canProject, Point, Polygon, Polyline, Graphic } from 'lark-map';
import SymbolUtils from "@/utils/yc/SymbolUtils";
/**
 * 图形工具类
 */
const GeometryUtils = {
	regExes: {
		typeStr: /^\s*(\w+)\s*\(\s*(.*)\s*\)\s*$/,
		spaces: /\s+/,
		parenComma: /\)\s*,\s*\(/,
		doubleParenComma: /\)\s*\)\s*,\s*\(\s*\(/,  // can't use {2} here
		trimParens: /^\s*\(?(.*?)\)?\s*$/
	},
	/** 
	 * wkt转化成arcgis的Polygon对象 
	 * @param wkt 
	 * @returns {Polygon} 
	 * @constructor 
	 */
	WktToPolygon(wkt, spatialreference) {
		var features, type, str;
		wkt = wkt.replace(/[\n\r]/, " ");
		var matches = this.regExes.typeStr.exec(wkt);
		if (matches) {
			type = matches[1].toLowerCase();
			str = matches[2];
			if (type === "multipolygon") {
				features = this.wktToMultipolygon(str, spatialreference);
			}
		}
		return features;
	},


	/**
		 * 创建WKT格式
		 * @param {*} geometry 
		 */
	multipolygonToWkt(geometry) {
		var wkt = [];
		var rings = geometry.rings;
		for (var i in rings) {
			var ring = rings[i];
			for (var j in ring) {
				var p = ring[j];
				wkt.push(p.join(" "));
			}
		}
		return "MULTIPOLYGON (((" + wkt.join(",") + ")))";
	},
	/**
	 * 转换点
	 * @param {*} str 
	 */
	wktToPoint(str) {
		var coords = this.trim(str).split(this.regExes.spaces);
		return [Number(coords[0]),Number(coords[1])];
	},
	/**
	 * 转换线
	 * @param {*} str 
	 */
	wktToPolyline(str, spatialreference) {
		var points = this.trim(str).split(',');
		var components = [];
		for (var i = 0, len = points.length; i < len; ++i) {
			let geometry = this.wktToPoint(points[i]);
			components.push(geometry);
		}
		let line = new Polyline({
			type: "polyline",
			spatialreference: spatialreference
		});
		line.addPath(components);
		return line;//new esri.geometry.Polyline(components);
	},
	/**
	 * 转换面对象
	 * @param {*} str 
	 */
	wktToPolygon(str, spatialreference) {
		var ring, linestring, linearring;
		var rings = this.trim(str).split(this.regExes.parenComma);
		var components = [];
		for (var i = 0, len = rings.length; i < len; ++i) {
			ring = rings[i].replace(this.regExes.trimParens, '$1');
			linestring = this.wktToPolyline(ring);
			components.push(linestring);
		}
		let polygon = new Polygon({
			type: "polygon",
			spatialreference: spatialreference,
		});
		polygon.addRing(components);
		return polygon;
	},
	/**
	 * 转换多面对象
	 * @param {*} str 
	 */
	wktToMultipolygon(str, spatialreference) {
		var ring, linestring, linearring;
		var rings = this.trim(str).split(this.regExes.parenComma);
		var components = [];
		for (var i = 0, len = rings.length; i < len; ++i) {
			ring = rings[i].replace(this.regExes.trimParens, '$1');
			if (ring.indexOf("(") > -1) {
				ring = ring.replace("(", "");
			}
			if (ring.indexOf(")") > -1) {
				ring = ring.replace(")", "");
			}
			linestring = this.wktToPolyline(ring);
			let paths = linestring.paths;
			components.push(paths[0]);
		}
		let polygon = new Polygon({
			type: "polygon",
			spatialreference: spatialreference,
		});
		polygon.addRing(components[0]);
		return polygon;
	},
	/**
	 * 
	 * @param {*} points            点集合
	 * @param {*} spatialReference  坐标系
	 * @param {*} symbol            样式
	 */
	toGraphic(points, spatialReference, symbol = SymbolUtils.HIGHLIGHT_RENDER) {
		let polygon = new Polygon({
			spatialReference: spatialReference,
		});
		polygon.addRing(points);
		let graphic = new Graphic({
			type: "polygon",
			geometry: polygon,
			symbol: symbol
		});
		return graphic;
	},

	/**
	 * 设置feature样式,根据PDJB字段
	 * @param {*} features 
	 */
	setSymbolByFeatures(features, spatialReference) {
		let graphics = [];
		if (features && features.length > 0) {
			for (let index = 0; index < features.length; index++) {
				let feature = features[index];
				let attributes = feature.attributes;
				let symbol;
				if (attributes.hasOwnProperty("PDJB")) {
					symbol = SymbolUtils.setRenderer(feature.attributes.PDJB);
				}else if(attributes.hasOwnProperty("SITE_TYPE")){
					symbol = SymbolUtils.setTZHRenderer(feature.attributes.SITE_TYPE);
				}else {
					symbol = SymbolUtils.HIGHLIGHT_RENDER;
				}
				let polygon = new Polygon({
					type: "polygon",
					rings: feature.geometry.rings,
					spatialReference: spatialReference
				});
				let graphic = new Graphic({
					attributes: feature.attributes,
					geometry: polygon,
					symbol: symbol
				});
				graphics.push(graphic);
			}
		}
		return graphics;
	},
	/**
	 * 根据经纬度和坐标系创建point图形
	 * @param {*} longitude 经度
	 * @param {*} latitude  纬度
	 * @param {*} kid       坐标系
	 */
	toPoint(longitude = 0, latitude = 0, kid = null) {
		let point = null;
		if (kid != null && kid != undefined && kid != "" && canProject(point, { wkid: 4490 })) {
			point = new Point({
				type: "point",
				x: longitude,
				y: latitude,
				spatialReference: {
					wkid: kid === null ? 4490 : kid
				}
			});
			webMercatorProject(poiint, { wkid: 4490 });
		} else {
			point = new Point({
				type: "point",
				x: longitude,
				y: latitude,
				spatialReference: {
					wkid: kid === null ? 4490 : kid
				}
			});
		}
		return point;
	},



	/**
	 * 度分秒转换经纬度
	 * @param {*} degree  度
	 * @param {*} minute  分
	 * @param {*} second  秒
	 */
	toDigital(degree, minute, second) {
		degree = (typeof (degree) == "undefined" || degree == "") ? 0 : parseFloat(degree);
		minute = (typeof (minute) == "undefined" || minute == "") ? 0 : parseFloat(minute) / 60;
		second = (typeof (second) == "undefined" || second == "") ? 0 : parseFloat(second) / 3600;
		let digital = degree + minute + second;
		if (digital == 0) {
			return "";
		} else {
			return digital;
		}
	},

	/**
	 * geojson转换graphic
	 * @param {*} geoJson 
	 * @param {*} spatialReference 
	 */
	geoJsonToGraphic(geoJson,spatialReference) {
		let owner = this;
		let graphics = null;
		if (geoJson != undefined && geoJson != "") {
			graphics = [];
			let data = JSON.parse(geoJson.data);
			let features = data["features"];
			let wkt = data["crs"]["name"].code;
			if (features && features.length > 0) {
				features.forEach(feature => {
					let coordinates = feature.geometry.coordinates;
					let polygon = new Polygon({
						type: "polygon",
						rings: coordinates[0],
						spatialReference: spatialReference
					});
					let attributes = owner.getAttributes(feature.properties);
					let graphic = new Graphic({
						attributes: attributes,
						geometry: polygon,
						symbol: SymbolUtils.DRAW_POLYGON_RENDER
					});
					graphics.push(graphic);
				});
			}
		}
		return graphics;
	},
	/**
	 * graphic转换geoJson
	 * @param {*} graphics 
	 */
	graphicToGeoJson(graphics, wkid = 4490) {
		if (graphics) {
			let geoJson = {
				"type": "FeatureCollection",
				"features": [],
				//"crs": { "type": "name", "properties": { "name": "EPSG:" + wkid + "" } },
			};
			graphics.forEach(graphic => {
				let feature = {
					"type": "Feature",
					"properties": graphic.attributes,
					"geometry": {
						"type": "Polygon",
						"coordinates": graphic.geometry.rings
					}
				};
				geoJson.features.push(feature);
			});
			return geoJson;
		}
	},

	/**
	 * 获取geojson属性对象
	 * @param {*} attributes 
	 */
	getAttributes(attributes) {
		let object = {};
		if (attributes) {
			for (const key in attributes) {
				switch (key) {
					case "COUNTY_COD":
						object["COUNTY_CODE"] = attributes[key];
						break;
					case "COUNTY_NAM":
						object["COUNTY_NAME"] = attributes[key];
						break;
					case "PROVINCE_C":
						object["PROVINCE_CODE"] = attributes[key];
						break;
					case "PROVINCE_N":
						object["PROVINCE_NAME"] = attributes[key];
						break;
					case "VILLAGE_AR":
						object["VILLAGE_AREA"] = attributes[key];
						break;
					default:
						object[key] = attributes[key];
						break;
				}
			}
			return object;
		}
	},
	trim(str) {
		return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	},

}
export default GeometryUtils;